<template>
<!--template类似于html的div盒子-->
  <el-container direction="vertical">
    <!--获取container布局，并且设置为垂直布局-->
    <el-form :model="goodsForm" :rules="rules" ref="goodsForm" label-width="auto" class="demo-ruleForm goodsForm" >
      <!--:model是语法糖，全称是v-on:model,其他同理，绑定class属性,demo-ruleForm" goodsForm-->
      <el-form-item label="物资名称" prop="name">
        <!--prop传值的内容对象是name-->
        <el-input prefix-icon="el-icon-goods" v-model="goodsForm.name" clearable placeholder="请输入物资名称"></el-input>
        <!--icon就是图标的意思，placeholder起到提示的作用-->
      </el-form-item>
      <el-form-item label="库存数量" prop="stock">
        <el-input prefix-icon="el-icon-goods" v-model="goodsForm.stock" clearable placeholer="请输入库存数量"></el-input>
      </el-form-item>
      <el-form-item label="物资价格" prop="price">
        <el-input prefix-icon="el-icon-goods" v-model="goodsForm.price" clearable placeholer="请输入库存价格"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="goodsForm.remark" placeholer="请输入库存价格"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('goodsForm')">添加</el-button>
        <el-button @click="resetForm('goodsForm')">重置</el-button>
        <el-button type="info" @click="$router.go(-1)">返回</el-button><!-- -1就是向上的意思 -->
      </el-form-item>
    </el-form>
  </el-container>
</template>

<script>
  import Qs from 'qs'
  /*将url中的参数转化为对象，将对象转化为url中的参数形式*/
  export default {
    name: "AddGoodsPage",
    data(){
      return{
        goodsForm:{},
        rules:{
          name:[
            {required:true,message:'请输入物资名称',trigger:'blur'}
          ],
          stock:[
            {required:true,message:'请输入库存数量',trigger:'blur'},
            {min:0,message: '库存数量不能小于0',trigger: 'blur'}
          ],
          price:[
            {required:true,message:'请输入价格',trigger:'blur'},
            {min:0,message: '价格不能小于0',trigger: 'blur'}
          ]
        }
      }
    },
    methods:{
      submitForm(forName){
        this.$refs[forName].validate((valid) => {
          if (valid){
            let params = Qs.stringify(this.goodsForm);
            this.$http.post('/goods/add',params)
              .then((response)=>{
                if(response.status === 200){
                  if(response.data.code === 10000){
                    this.$notify({
                      title: '成功',
                      message: "物资Id是:"+response.data.data.id,
                      duration: 1500
                    })
                  }else{
                    this.$notify.error({
                      title: '错误',
                      message: response.data.msg,
                      duration: 1000
                    });
                  }
                }else{
                  this.$notify.error({
                    title: '错误',
                    message: response.status + response.statusText,
                    duration: 1000
                  });
                }
              });
          }
        });
      },
      resetForm(forName){
        this.$refs[forName].resetFields();
      }
    }
  }
</script>

<style scoped>

</style>
